
using System.IO;

namespace Beaker.Services.FileSystem.Zip
{
	public class ZipFileSystemServiceProvider
		: IFileSystemServiceProvider
	{
		/// <summary>
		/// Tries to convert the file to a filesystem service, if possible.
		/// </summary>
		/// <param name="file">The file to convert to a filesystem.</param>
		/// <returns>A FileSystemService, or <c>null</c> if the file cannot be converted.</returns>
		public IFileSystemService FromFile( IFile file )
		{
			if( file == null )
				return null;

			// if it is a zip file, return a ZIP IFileSystemService
			bool isZip = false;
			var buffer = new byte[4];
			using( Stream stream = file.OpenRead() )
			{
				if( stream.Read( buffer, 0, 4 ) == 4 )
				{
					if( buffer[0] == 0x50 && buffer[1] == 0x4b && buffer[2] == 0x03 && buffer[3] == 0x04 )
						isZip = true;
				}
			}

			if( isZip )
				return new ZipFileSystemService( file );

			return null;
		}
	}
}